﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.BLL.GlobalCommands;

namespace MS.InformationManagement.BLL.CaptureModule
{
    public class CaptureProcess : IBaseCommand
    {

        #region fields

        int _idSelectedItem;
        List<IBaseCommand> _commands;
        Office.DocumentSet _currenDocument;
        SPWeb _currentWeb;
        SPList _currentList;
        FileEntity _fileContent;
        int _currentIndexTransaction;
        string _currentUser;
        string _currentSiteUrl;

        #endregion

        #region Constructors

        public CaptureProcess(int idSelectedItemToCapture, FileEntity fileContent, string currentSiteUrl)
        {
            _idSelectedItem = idSelectedItemToCapture;
            _fileContent = fileContent;
            _commands = new List<IBaseCommand>();
            _currentSiteUrl = currentSiteUrl;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
             _currentUser = SPContext.Current.Web.CurrentUser.LoginName;

             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                using (SPSite currentSite = new SPSite(_currentSiteUrl))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        try
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            _currentWeb = currentWeb;
                            _currentList = _currentWeb.Lists[GlobalConstants.LIST_RADICADOS_ENTRANTES];
                            initCommandValues();

                            for (_currentIndexTransaction = 0; _currentIndexTransaction < _commands.Count; _currentIndexTransaction++)
                                _commands[_currentIndexTransaction].Execute();

                            _currentList.Update();
                            _currentWeb.AllowUnsafeUpdates = false;
                        }
                        catch (Exception ex)
                        {
                            Undo();
                            throw ex;
                        }
                    }
                }
            });
        }

        public void Undo()
        {
            for (int i = 0; i <= _currentIndexTransaction; i++)
                _commands[_currentIndexTransaction].Undo(); 
        }

        #endregion

        #region Private Methods

        private void initCommandValues()
        {
            //Init variables
            SPListItem selectdItem = _currentList.GetItemById(_idSelectedItem);
            string selectedRadicado = selectdItem[CustomSiteColumns.FIELD_INCOMMING_RADICADO].ToString();
            string ownerLoginName = selectdItem[CustomSiteColumns.FIELD_INCOMMING_OWNER].ToString();
            _currenDocument = RadicadosHelper.TryGetRadicado(selectedRadicado, ownerLoginName, _currentWeb);    

            //Creating transactions
            _commands.Add(new LoadCapturedImageRadicadoCommand(_fileContent, _currenDocument));
            _commands.Add(new UpdateCapturedItemCommand(_currenDocument, _currentUser));
            _commands.Add(new ChangeStateCommand(_currenDocument, GlobalConstants.METADATA_TERM_STATUS_CAPTURED));
            _commands.Add(new DeleteCaptureItemCommand(selectdItem));
        }

        #endregion
    }
}
